package com.nbsaas.boot.tenant.ext.resource;

import com.nbsaas.boot.rest.filter.Filter;
import com.nbsaas.boot.rest.response.ResponseObject;
import com.nbsaas.boot.tenant.api.apis.DatabaseApi;
import com.nbsaas.boot.tenant.api.domain.field.DatabaseField;
import com.nbsaas.boot.tenant.api.domain.simple.DatabaseSimple;
import com.nbsaas.boot.tenant.ext.apis.DatabaseExtApi;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;


@Service
public class DatabaseExtResource implements DatabaseExtApi {

    @Resource
    private DatabaseApi databaseApi;

    @Transactional(readOnly = true)
    @Override
    public ResponseObject<DatabaseSimple> selectByDomain(String domain) {
        ResponseObject<DatabaseSimple> result=new ResponseObject<>();
        List<DatabaseSimple> list = databaseApi.listData(Filter.eq(DatabaseField.domain, domain));
        if(list==null||list.isEmpty()){
            result.setCode(501);
            result.setMsg("未找到数据");
            return result;
        }
        result.setData(list.get(0));
        return result;
    }
}
